package com.app.repository.impl;

import com.app.model.main.Article;
import com.app.repository.interfaces.ArticleMapper;
import com.app.support.dao.MybatisDao;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lichaojie on 2017/3/12. It's a beautiful day.
 */
@Repository
public class ArticleMapperImpl implements ArticleMapper {

    @Autowired
    private MybatisDao mybatisDao;

    @Override
    public int deleteByPrimaryKey(Long id){
        return mybatisDao.delete("com.app.dao.main.ArticleMapper",".deleteByPrimaryKey",id);
    }

    @Override
    public int insert(Article record){
        return mybatisDao.insert("com.app.dao.main.ArticleMapper", ".insert", record);
    }

    @Override
    public int insertSelective(Article record){
        return mybatisDao.insert("com.app.dao.main.ArticleMapper", ".insertSelective", record);
    }

    @Override
    public Article selectByParam(long userId, String time){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("time",time);
        return mybatisDao.get("com.app.dao.main.ArticleMapper",".selectByParam",map);
    }

    @Override
    public Article selectByPrimaryKey(Long id){
        return mybatisDao.get("com.app.dao.main.ArticleMapper", ".selectByPrimaryKey", id);
    }

    @Override
    public int updateByPrimaryKeySelective(Article record){
        return mybatisDao.update("com.app.dao.main.ArticleMapper", ".updateByPrimaryKeySelective", record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(Article record){
        return mybatisDao.update("com.app.dao.main.ArticleMapper", ".updateByPrimaryKeyWithBLOBs", record);
    }

    @Override
    public int updateByPrimaryKey(Article record){
        return mybatisDao.update("com.app.dao.main.ArticleMapper",".updateByPrimaryKey",record);
    }

    @Override
    public List<Article> getListOrderedByIdDESC(long userId, long lastId, int page, int number, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("lastId",lastId);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper", ".getListOrderedByIdDESC", map, new RowBounds(0, number));
    }

    @Override
    public List<Article> getListOrderedByIdASC(long userId, long lastId, int page, int number, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("lastId",lastId);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper", ".getListOrderedByIdASC", map, new RowBounds(0, number));
    }

    @Override
    public List<Article> getListOrderedByIdWithoutUserIdDESC(long lastId, int page, int number, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("lastId",lastId);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getListOrderedByIdWithoutUserIdDESC",map,new RowBounds(0,number));
    }

    @Override
    public List<Article> getListOrderedByIdWithoutUserIdASC(long lastId, int page, int number, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("lastId",lastId);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getListOrderedByIdWithoutUserIdASC",map,new RowBounds(0,number));
    }
    @Override
    public long getMaxId(){
        return mybatisDao.count("com.app.dao.main.ArticleMapper", ".getMaxId", null);
    }

    @Override
    public List<Article> getIdListBetween(String time1, String time2, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("time1",time1);
        map.put("time2",time2);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getIdListBetween",map);
    }

    @Override
    public List<Article> getListOrderedByTimeWithoutUserIdDESC(String time, int page, int number, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("time",time);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getListOrderedByTimeWithoutUserIdDESC",map,new RowBounds(0,number));
    }

    @Override
    public List<Article> getListOrderedByTimeWithoutUserIdASC(String time , int page, int number, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("time",time);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getListOrderedByTimeWithoutUserIdASC",map,new RowBounds(0,number));
    }

    @Override
    public long getArticleNumber(long userId,byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("type",type);
        return mybatisDao.count("com.app.dao.main.ArticleMapper",".getArticleNumber",map);
    }

    @Override
    public List<Article> getArticleList(long userId, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getArticleList",map);
    }

    @Override
    public int updateFlagByPrimaryKey(long id){
        return mybatisDao.update("com.app.dao.main.ArticleMapper",".updateFlagByPrimaryKey",id);
    }

    @Override
    public int updateArticleType(long id, byte type) {
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("type",type);
        return mybatisDao.update("com.app.dao.main.ArticleMapper",".updateArticleType",map);
    }

    @Override
    public List<Article> getByType(Byte type) {
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getByType",type);
    }

    @Override
    public List<Long> getUnexpiredArticle(Byte type, String expiredTimeSub) {
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        map.put("expiredTimeSub",expiredTimeSub);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getUnexpiredArticle",map);
    }

    @Override
    public List<Long> getExpiredArticle(Byte type, String expiredTimeSub) {
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        map.put("expiredTimeSub",expiredTimeSub);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getExpiredArticle",map);
    }

    @Override
    public List<Long> getExceptTopArticleByType(byte type, byte top) {
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        map.put("top",top);
        return mybatisDao.getList("com.app.dao.main.ArticleMapper",".getExceptTopArticleByType",map);
    }
}
